package com.taptech.doufu.ui.view;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.bean.ProductionBean;
import com.taptech.doufu.bean.cartoon.CartoonBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.SweepFlowerBean;
import com.taptech.doufu.services.GiftService;
import com.taptech.doufu.ui.activity.FlowerRankActivity;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.net.httputils.HttpResponseObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by longsiyang on 16/1/8.
 */
public class FlowerAndGiftManager<T extends ProductionBean> implements View.OnClickListener , HttpResponseListener {

    static private Context mContext;
    static private FlowerAndGiftManager flowerAndGiftViewManager;
    private T bean;

    // UI View
    private RelativeLayout giveFlowerBtn , giveGiftBtn;
    private LinearLayout flowerUserBtn , giftUserBtn;
    private TextView flowerCountTextView , giftCountTextView;
    private List<ImageView> flowerUserIconImageView , giftUserIconImageView;
    private RelativeLayout giftViewLayout;
    private View diliverLineView;

    // View manager
    FlowerDialogManager flowerDialogManager;

    static public FlowerAndGiftManager initInView(Context context, View rootview) {

        flowerAndGiftViewManager = new FlowerAndGiftManager();
        mContext = context;
        flowerAndGiftViewManager.initData();
        flowerAndGiftViewManager.initView(rootview);
        return flowerAndGiftViewManager;

    }



    private void initData(){


    }

    private void initView(View rootview){
        if (rootview == null){
            new Throwable("rootview can't be null");
        }
        flowerUserIconImageView = new ArrayList<>();
        giftUserIconImageView = new ArrayList<>();

        flowerCountTextView = (TextView) rootview.findViewById(R.id.flower_give_count);
        giftCountTextView = (TextView) rootview.findViewById(R.id.gift_give_count);
        // 可点击区域
        giveFlowerBtn = (RelativeLayout) rootview.findViewById(R.id.flower_top_layout);
        giveGiftBtn = (RelativeLayout) rootview.findViewById(R.id.gift_top_layout);
        flowerUserBtn = (LinearLayout) rootview.findViewById(R.id.flower_user_icon_layout);
        giftUserBtn = (LinearLayout) rootview.findViewById(R.id.gift_user_icon_layout);
        // 打赏整体和分隔线，用于控制显示与隐藏
        diliverLineView = rootview.findViewById(R.id.diliver_line_view);
        giftViewLayout = (RelativeLayout) rootview.findViewById(R.id.gift_view_layout);

    }


    public void setFlowerAndGiftManagerInfo(T bean){

        if (bean != null){
            this.bean = bean;
            giveFlowerBtn.setOnClickListener(this);
            giveGiftBtn.setOnClickListener(this);
            flowerUserBtn.setOnClickListener(this);
            giftUserBtn.setOnClickListener(this);
            if (bean instanceof HomeTopBean){
                refreshInfo(((HomeTopBean) bean).getFlowerCount() , bean.getReward_sum() , bean.getFlowerers() , bean.getRewarders());
            }
            else if (bean instanceof CartoonBean){
                refreshInfo(((CartoonBean)bean).getFlower_num() , bean.getReward_sum(), bean.getFlowerers() , bean.getRewarders());
            }
            else {
                refreshInfo(bean.getFlower_count() , bean.getReward_sum(), bean.getFlowerers() , bean.getRewarders());
            }

        }

    }

    public void refreshInfo(String flowerCount , String giftCount , SweepFlowerBean[] flowerUserBeans , SweepFlowerBean[] giftUserBeans){
        if (flowerCountTextView != null ){
            flowerCountTextView.setText("献花 "+flowerCount);
        }
        if (giftCountTextView != null){
            giftCountTextView.setText("打赏 "+giftCount);
        }
        if (flowerUserBeans != null){
            for (int i = 0 ; i < flowerUserBeans.length ; i++){
                if (flowerUserIconImageView != null){
                    ImageView headImg = new ImageView(mContext);
                    initRoundIconView(headImg);
                    flowerUserIconImageView.add(headImg);
                    flowerUserBtn.addView(headImg);
                    GlideUtil.displayCircleImage(flowerUserIconImageView.get(i), flowerUserBeans[i].getUser().getIcon());
                }
            }
        }
        if (giftUserBeans != null){
            for (int i = 0 ; i < giftUserBeans.length ; i++){
                if (giftUserIconImageView != null){
                    ImageView headImg = new ImageView(mContext);
                    initRoundIconView(headImg);
                    if(WeMediaApplication.getInstance().isDayNightMode_Night){
                        headImg.setAlpha(Constant.imagViewAlpha);
                    }
                    giftUserIconImageView.add(headImg);
                    giftUserBtn.addView(headImg);
                    GlideUtil.displayCircleImage(giftUserIconImageView.get(i), giftUserBeans[i].getUser().getIcon());
                }
            }
        }

    }

    private void initRoundIconView(ImageView img){
        if (img != null){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ScreenUtil.dip2px(mContext , 31) , ScreenUtil.dip2px(mContext , 31));
            params.setMargins(ScreenUtil.dip2px(mContext , 5),0,ScreenUtil.dip2px(mContext , 5),0);
            params.gravity = Gravity.CENTER;
            img.setLayoutParams(params);
           if(WeMediaApplication.getInstance().isDayNightMode_Night){
               img.setAlpha(Constant.imagViewAlpha);
           }
        }
    }


    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.flower_top_layout:
                if (bean != null){
                    Map<String , String> map = new HashMap<>();
                    map.put("mid" , bean.getId());
                    map.put("mtype" , bean.getObject_type());
                    TMAnalysis.event(mContext , "open_flower_dialog" , map);
                    try {// Unable to add window -- token null is not valid; is your activity running?
                        flowerDialogManager = FlowerDialogManager.getInstance(mContext , bean);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
                break;
            case R.id.gift_top_layout:
                if (bean != null){
                    Map<String , String> map = new HashMap<>();
                    map.put("mid" , bean.getId());
                    map.put("mtype" , bean.getObject_type());
                    TMAnalysis.event(mContext , "open_gift_dialog" , map);
                    GiftDialogManager.getInstance(mContext , bean);
                }
                //GiftService.enterGiftListActivity(mContext , bean);
                break;
            case R.id.flower_user_icon_layout:
                if (bean != null){
                    Intent rankFlower = new Intent(mContext, FlowerRankActivity.class);
                    if (bean.getObject_type().equals("5")){
                        rankFlower.putExtra(Constant.OBJECT_TYPE , "18");
                    }else {
                        rankFlower.putExtra(Constant.OBJECT_TYPE, bean.getObject_type());
                    }
                    rankFlower.putExtra(Constant.NOTE_ID, bean.getId());
                    Map<String , String> map = new HashMap<>();
                    map.put("mid" , bean.getId());
                    map.put("mtype" , bean.getObject_type());
                    TMAnalysis.event(mContext, "open_flower_list", map);
                    mContext.startActivity(rankFlower);
                }
                break;
            case R.id.gift_user_icon_layout:
                Map<String , String> map = new HashMap<>();
                map.put("mid" , bean.getId());
                map.put("mtype" , bean.getObject_type());
                TMAnalysis.event(mContext, "open_gift_list", map);
                GiftService.enterGiftListActivity(mContext , bean);
                break;
        }

    }

    public void setGiftLayoutVisiable(boolean visiable){
        if (visiable){
            diliverLineView.setVisibility(View.VISIBLE);
            giftViewLayout .setVisibility(View.VISIBLE);
        }else {
            diliverLineView.setVisibility(View.GONE);
            giftViewLayout .setVisibility(View.GONE);
        }
    }
}
